"""
单线程爬虫
auth:xgt-python
datetime:2021/11/7
"""

import requests
import parsel   # 数据解析模块
import re
import time

def change_title(name):
    """替换的特殊字符"""
    mode = re.compile(r'[\\\/\:\*\?\<\>\"\|]')
    new_name = re.sub(mode,'_',name)
    return new_name


start_time = time.time()
for page in range(1,11):
    url = 'https://fabiaoqing.com/biaoqing/lists/page/{page}.html'

    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36'
    }
    response = requests.get(url=url,headers=headers)
    # print(response.text)
    # 获取response.text 是字符串数据 html 字符串数据
    # 用re直接进行解析
    # css选择器 是需要 把获取到的htnl字符串数据 转成selactor
    selector  = parsel.Selector(response.text)
    # css选择器是根据标签内容
    # getall() 获取所有的标签属性内容  返回是列表
    href = selector.css('#bqb div.ui.segment.imghover img::attr(data-original)').getall()
    names = selector.css('#bqb div.ui.segment.imghover img::attr(title)').getall()
    #   从列表里面  把元素一一提取出来
    # 遍历
    for index in zip(href,names):
        img_url = index[0]
        img_title = change_title(index[1])
        suffix = img_url.split('.')[-1]  #图片后缀名字
        print(img_title,suffix,img_url,sep=" | ")
        # 保存数据
        try:
            img_content = requests.get(url=img_url,headers=headers).content
            with open('img\\' + img_title + '.' + suffix,mode='wb') as file:
                file.write(img_content)
                print('正在保存:',img_title)
        except:
            pass


end_time = time.time()
use_time = end_time - start_time
print('总计耗时:', use_time)
